package com.lab.tools;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.ViewParent;
import android.view.WindowManager;

import com.lab.logtrack.AppLog;
import com.lab.tools.ui.HttpLog;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 用于管理桌面悬浮窗
 * Created by ak
 */
public class FloatWindowManager {


    public static final String FLOAT_MAIN_WINDOW_ADDED = "fm.xiami.bc.float_main_window_added"; // 主浮动窗口已显示。

    /**
     * 悬浮气泡的实例
     */
    private static FloatWindowView smallWindow;

    /**
     * 悬浮窗主界面的实例
     */
    private static FloatMainWindowView mainWindow;

    /**
     * 悬浮气泡的参数
     */
    private static WindowManager.LayoutParams smallWindowParams;

    /**
     * 悬浮窗主界面的参数
     */
    private static WindowManager.LayoutParams mainWindowParams;

    /**
     * 用于控制在屏幕上添加或移除悬浮窗
     */
    private static WindowManager mWindowManager;


    /**
     * 清理日志悬浮窗口
     */
//    private static FloatLogMainWindowView mLogWindow;

    /**
     * 日志悬浮气泡的参数
     */
    private static WindowManager.LayoutParams smallLogWindowParams;

    /**
     * 日志悬浮气泡的实例
     */
    private static FloatWindowView logSmallWindow;

    private static CopyOnWriteArrayList<HttpLog> logList = new CopyOnWriteArrayList<>();


    /**
     * 创建一个日志悬浮气泡。初始位置为屏幕的右部中间位置。
     *
     * @param application 应用程序的application.
     */

    public static void showFloatSmallWindowView(Context application) {
        if (application == null) {
            return;
        }
        WindowManager windowManager = getWindowManager(application.getApplicationContext());
        if (smallWindow == null) {
            smallWindow = new FloatWindowView(application.getApplicationContext());
            if (smallWindowParams == null) {
                smallWindowParams = new WindowManager.LayoutParams();
                smallWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
                smallWindowParams.format = PixelFormat.RGBA_8888;
                smallWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;

                DisplayMetrics dm = new DisplayMetrics();
                windowManager.getDefaultDisplay().getMetrics(dm);
                smallWindowParams.x = 0;
                smallWindowParams.y = dm.heightPixels / 2;

                smallWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
                smallWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            }

            try {
                smallWindow.setParams(smallWindowParams);
                windowManager.addView(smallWindow, smallWindowParams);
            } catch (IllegalStateException e) {
                AppLog.w("float view already exist");
            } catch (WindowManager.BadTokenException e) {
                AppLog.w("float view already exist");
            }

        }

//        showLogFloatSmallWindowView(application);
    }

    /**
     * 将日志悬浮气泡从屏幕上移除。
     *
     * @param context 必须为应用程序的Context.
     */
    public static void removeSmallWindow(Context context) {
        if (smallWindow != null) {
            WindowManager windowManager = getWindowManager(context);
            ViewParent view = smallWindow.getParent();
            if (view != null)
                windowManager.removeView(smallWindow);
            smallWindow = null;
        }
    }


    /**
     * 创建一个log悬浮窗主窗口，位置在屏幕正中间
     */
    public static void showFloatMainWindowView(Context application) {
        if (application == null) {
            return;
        }
        WindowManager windowManager = getWindowManager(application.getApplicationContext());
        if (mainWindow == null) {
            mainWindow = new FloatMainWindowView(application.getApplicationContext());
            if (mainWindowParams == null) {
                mainWindowParams = new WindowManager.LayoutParams();
                mainWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
                mainWindowParams.format = PixelFormat.RGBA_8888;
                mainWindowParams.gravity = Gravity.LEFT | Gravity.TOP;

                DisplayMetrics dm = new DisplayMetrics();
                windowManager.getDefaultDisplay().getMetrics(dm);
                mainWindowParams.x = 0;
                mainWindowParams.y = 0;

                mainWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                mainWindowParams.height = WindowManager.LayoutParams.MATCH_PARENT;
            }
            try {
                windowManager.addView(mainWindow, mainWindowParams);
                application.sendBroadcast(new Intent(FLOAT_MAIN_WINDOW_ADDED));
            } catch (IllegalStateException e) {
                AppLog.w("float view already exist");
            }
        }
        mainWindow.setHttpLogs(logList);
    }


    /**
     * 将悬浮窗主窗口从屏幕上移除。
     *
     * @param context 必须为应用程序的Context.
     */
    public static void removeFloatMainWindow(Context context) {
        if (mainWindow != null) {
            WindowManager windowManager = getWindowManager(context);
            windowManager.removeView(mainWindow);
            mainWindow = null;
        }
    }

    /**
     * 是否有悬浮窗显示在屏幕上。
     *
     * @return 有悬浮窗显示在桌面上返回true，没有的话返回false。
     */
    public static boolean isWindowShowing() {
        return smallWindow != null;
    }

    /**
     * 如果WindowManager还未创建，则创建一个新的WindowManager返回。否则返回当前已创建的WindowManager。
     *
     * @param context 必须为应用程序的Context.
     * @return WindowManager的实例，用于控制在屏幕上添加或移除悬浮窗。
     */
    private static WindowManager getWindowManager(Context context) {
        if (mWindowManager == null) {
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        return mWindowManager;
    }


}
